package com.ky.manage.activity.outdoor;

import android.content.Intent;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ky.manage.R;
import com.ky.manage.adapter.PicShowAdapter;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.model.response.OutdoorCheckItemListResp;
import com.ky.manage.model.response.OutdoorMendListResp;
import com.ky.manage.ui.CheckItemView;
import com.ky.manage.utils.ToastUtils;
import com.ky.manage.utils.ZyUtils;
import com.ky.manage.utils.http.BusinessHttpReqUtils;
import com.loopj.android.http.TextHttpResponseHandler;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * 户外安检/整改详情页
 */
public class OutdoorDetailActivity extends BaseActivity {
    private TextView checkPersonNameTv, checkPersonTelTv, finishTimeTv, reportAddressTv;
    private CheckItemView isNormalCheckItemView, isMendCheckItemView;
    private RecyclerView picListView;
    private LinearLayout checkListViewGroup;
    private RefreshLayout refreshLayout;

    private OutdoorMendListResp.OutdoorMendInfo outdoorMendInfo;

    public static void toDetailPage(BaseActivity baseActivity, OutdoorMendListResp.OutdoorMendInfo outdoorMendInfo) {
        Intent intent = new Intent(baseActivity, OutdoorDetailActivity.class);
        intent.putExtra("outdoorMendInfo", outdoorMendInfo);
        baseActivity.toActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_outdoor_detail;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });
        refreshLayout = findView(R.id.refreshLayout);
        checkPersonNameTv = findView(R.id.check_name_tv);
        checkPersonTelTv = findView(R.id.tel_tv);
        finishTimeTv = findView(R.id.finish_time_tv);
        reportAddressTv = findView(R.id.report_address_tv);
        isNormalCheckItemView = findView(R.id.is_normal_check_item_view);
        isMendCheckItemView = findView(R.id.is_mend_check_item_view);
        picListView = findView(R.id.pic_list_view);
        checkListViewGroup = findView(R.id.check_list_ll);
    }

    @Override
    protected void initData() {
        outdoorMendInfo = (OutdoorMendListResp.OutdoorMendInfo)getIntent().getSerializableExtra("outdoorMendInfo");
        if (null == outdoorMendInfo) {
            return;
        }
        findTextView(R.id.page_title_tv, outdoorMendInfo.lineName);
        checkPersonNameTv.setText(outdoorMendInfo.checkPersonName);
        checkPersonTelTv.setText(outdoorMendInfo.checkPersonTel);
        finishTimeTv.setText(outdoorMendInfo.finishTime);

        isNormalCheckItemView.setLabelTxt("是否正常");
        isNormalCheckItemView.setStateTxt("0".equals(outdoorMendInfo.status) ? "是" : "否");
        isNormalCheckItemView.setShowArrowRightIv(false);
        isMendCheckItemView.setLabelTxt("是否整改");
        isMendCheckItemView.setStateTxt("0".equals(outdoorMendInfo.isMend) ? "是" : "否");
        isMendCheckItemView.setShowArrowRightIv(false);

        refreshLayout.setRefreshHeader(new ClassicsHeader(this));
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));
        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            reqOutdoorCheckListInfo();
        });

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        picListView.setLayoutManager(gridLayoutManager);
        PicShowAdapter picShowAdapter = new PicShowAdapter(R.layout.layout_pic_show_item,
                null == outdoorMendInfo.docList ? new ArrayList<>() : outdoorMendInfo.docList);
        picListView.setAdapter(picShowAdapter);
        picShowAdapter.notifyDataSetChanged();
    }

    private void reqOutdoorCheckListInfo() {
        if (!ZyUtils.getInstance().isNetConnected()) {
            refreshLayout.finishRefresh();
            return;
        }
        BusinessHttpReqUtils.getInstance().requestOutdoorCheckItemListInfo(outdoorMendInfo.lineId, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                ZyUtils.getInstance().insertLog("getProjectList", "onFailure statusCode = "
                        + statusCode + ", responseString = " + responseString);
                ToastUtils.showRoundRectToast("服务器异常，获取检测项目失败");
                refreshLayout.finishRefresh();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                ZyUtils.getInstance().insertLog("getProjectList", "onSuccess statusCode = "
                        + statusCode + ", responseString = " + responseString);
                refreshLayout.finishRefresh();
                if (statusCode == 200) {
                    OutdoorCheckItemListResp itemListResp = BusinessHttpReqUtils.getInstance().parseJsonObj(responseString, OutdoorCheckItemListResp.class);
                    if (null != itemListResp && itemListResp.code == 200) {
                        initCheckListResult(itemListResp.data);
                    } else {
                        ToastUtils.showRoundRectToast("服务器异常，获取检测项目失败");
                    }
                } else {
                    ToastUtils.showRoundRectToast("服务器异常，获取检测项目失败");
                }
            }
        });
    }

    /**
     * 检测条目列表
     * @param rows
     */
    private void initCheckListResult(List<OutdoorCheckItemListResp.CheckItemInfo> rows) {
        checkListViewGroup.removeAllViews();
        if (null != rows && !rows.isEmpty()) {
            for (int i = 0; i < rows.size(); i++) {
                OutdoorCheckItemListResp.CheckItemInfo checkItemInfo = rows.get(i);
                CheckItemView checkItemView = new CheckItemView(this);
                checkItemView.setShowArrowRightIv(false);
                checkItemView.setLabelTxt(checkItemInfo.projectName);
                //是否有问题（0是， 1否）
                checkItemView.setStateTxt("0".equals(checkItemInfo.isQuestion) ? "是" : "否");
                checkListViewGroup.addView(checkItemView);
            }
        }
    }
}